package com.xiaoyu.merchant.ui.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.merchant.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @date 创建时间 2018/10/10
 * @author jx
 * @Description  引导页
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.guide_view_pager)
    ViewPager mViewPager;

    private FrameLayout[] mGuideViews;
    private int[] mImageRes = new int[]{R.mipmap.app_guide_page1, R.mipmap.app_guide_page2, R.mipmap.app_guide_page3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        hideOccupyView();
        ButterKnife.bind(this);
        initViewPager();
    }

    //初始化
    private void initViewPager() {
        mGuideViews = new FrameLayout[mImageRes.length];
        GuideViewPagerAdapter adapter = new GuideViewPagerAdapter();
        mViewPager.setAdapter(adapter);
    }

    //跳转密码登录页面
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.guide_enter) {
            PreferencesUtil.commit(PreferencesUtil.KEY_HAS_GUIDED, true);
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }

        if(view.getId() == R.id.guide_img){
            PreferencesUtil.commit(PreferencesUtil.KEY_HAS_GUIDED, true);
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
    }


    //内部适配器
    public class GuideViewPagerAdapter extends PagerAdapter {
        @BindView(R.id.guide_img)
        ImageView guideImg;
        @BindView(R.id.guide_enter)
        ImageView guideEnter;

        @Override
        public int getCount() {
            return mImageRes.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mGuideViews[position]);
            mGuideViews[position] = null;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (mGuideViews[position] == null) {
                mGuideViews[position] = (FrameLayout) LayoutInflater.from(container.getContext())
                        .inflate(R.layout.app_guide_item, container, false);

                guideImg = mGuideViews[position].findViewById(R.id.guide_img);
                guideImg.setImageResource(mImageRes[position]);
                guideImg.setOnClickListener(GuideActivity.this);

                guideEnter = mGuideViews[position].findViewById(R.id.guide_enter);
                if (position == mImageRes.length - 1) {
                    guideEnter.setVisibility(View.VISIBLE);
                    guideEnter.setOnClickListener(GuideActivity.this);
                }
            }
            container.addView(mGuideViews[position], 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));//添加页卡
            return mGuideViews[position];

        }
    }
}
